﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using WebTemplate.DAL.Repositories;
using WebTemplate.DAL.Entities;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.Net.Mail;
using System.Net;
using System.Web.UI.WebControls;

namespace WebTemplate.ControlPanel.Membership
{
    public partial class MembersList : System.Web.UI.Page
    {


        #region Events



        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                FillResult();
            }
        }

        protected void Members_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.PageIndex = e.NewPageIndex;
            FillResult();
        }

        protected void Members_Sorting(object sender, GridViewSortEventArgs e)
        {
            this.SortExpression = e.SortExpression;
                this.PageIndex = 0;
            FillResult();
        }

        protected void Members_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "DeleteMember")
            {
                int rowindex = int.Parse(e.CommandArgument.ToString());
                int memberid = (int)Members.DataKeys[rowindex].Value;
                MemberReprositry reprositry = new MemberReprositry();
                reprositry.Delete(memberid);
                FillResult();
            }

            if (e.CommandName == "ApproveMember")
            {
                int rowindex = int.Parse(e.CommandArgument.ToString());
                int memberid = (int)Members.DataKeys[rowindex].Value;
                MemberReprositry reprositry = new MemberReprositry();
                MembersSearchCriteria search = new MembersSearchCriteria();
                search.Id = memberid;
                Members member =  reprositry.Select(search).First();
                if (member != null)
                {
                    member.IsActive = true;
                    reprositry.Update(member);
                    SendMail(member);
                    FillResult();
                }
            }
        }



        #endregion

        #region

        private MembersSearchCriteria SearchCriteria
        {
            get
            {
                return this.ViewState["SearchCriteria"] as MembersSearchCriteria;
            }

            set
            {
                this.ViewState["SearchCriteria"] = value;
            }
        }

        private string SortExpression
        {
            set
            {
                this.ViewState["SortExpression"] = value;
            }
            get
            {
                return this.ViewState["SortExpression"] as string;
            }
        }

        private int PageIndex
        {
            set
            {
                this.ViewState["PageIndex"] = value;
            }
            get
            {
                return this.ViewState["PageIndex"] == null ? 0 : (int)this.ViewState["PageIndex"];
            }
        }

        private void FillResult()
        {
            var searchCriteria = this.SearchCriteria;
            var sortExpression = this.SortExpression;

            if (searchCriteria == null)
            {
                searchCriteria = new MembersSearchCriteria();
                this.SearchCriteria = searchCriteria;
            }

            if (string.IsNullOrWhiteSpace(sortExpression))
            {
                sortExpression = this.SortExpression = "Id";
            }
            MemberReprositry reprositry = new MemberReprositry();
            IEnumerable<Members> result = reprositry.Select(searchCriteria);

            if (sortExpression == "UserID")
                result = result.OrderBy(x => x.UserID).ToList();
            else if (sortExpression == "FirstName")
                result = result.OrderBy(x => x.FirstName).ToList();
            else if (sortExpression == "LastName")
                result = result.OrderBy(x => x.LastName).ToList();
            else if (sortExpression == "Email")
                result = result.OrderBy(x => x.Email).ToList();
            else if (sortExpression == "IsActive")
                result = result.OrderBy(x => x.IsActive).ToList();
            Members.PageIndex = this.PageIndex;
            Members.DataSource = result.ToList();
            Members.DataBind();
        }

        public void SendMail(Members sender)
        {
            MailMessage message = new MailMessage();
            message.To.Add(new MailAddress(sender.Email));
            message.Subject = "Welcome to webtemplate";
            message.Body = "successfully";
            SmtpClient client = new SmtpClient();
            client.Send(message);
        }

        #endregion
    }
}